package cn.christ.com.mobilesafe.service;


import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.telephony.SmsManager;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;

import java.util.List;

import cn.christ.com.mobilesafe.common.utils.LogUtils;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;

public class GPSService extends Service {
    private MListener mListener;
    private LocationManager mLm;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.d("GPSService:onCreate");
        // 1 获取LocationManager的对象
        mLm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // 2 添加权限
        /*
         * <uses-permission
		 * android:name="android.permission.ACCESS_FINE_LOCATION" />
		 */
        // 3 调用请求位置的方法：requestLocationUpdates(String provider, long minTime,
        // float minDistance,LocationListener listener)
        // provider:需要用那种请求方式
        // minTime:最小更新时间，单位毫秒
        // minDistance:最小更新距离，单位是米
        // listener:位置回调监听
        mListener = new MListener();

        AndPermission.with(getApplicationContext())
                .requestCode(Constants.ACCESS_FINE_LOCATION)
                .permission(Manifest.permission.ACCESS_FINE_LOCATION)
                .callback(new PermissionListener() {
                    @Override
                    public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                        if (Constants.ACCESS_FINE_LOCATION == requestCode) {
                            checkSelfPermission();
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                    }
                })
                .start();
    }

    private void checkSelfPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mLm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, mListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mListener != null) {
            mLm.removeUpdates(mListener);
        }
    }

    public class MListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            // 位置发生改变
            // 获取经纬度
            double longitude = location.getLongitude();// 经度
            double latitude = location.getLatitude();// 维度
            // 发送短信给安全号码
            String safeNum = (String) SPUtils.get(Constants.SJFD_SAFE_NUM, "");
            SmsManager.getDefault().sendTextMessage(safeNum, null,
                    "longitude:" + longitude + ";" + "latitude:" + latitude, null, null);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }

}
